<?php
namespace Home\Controller;

use Think\Controller;

class UtilController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        Vendor('Wxmpay.Pubhelper');
        Vendor('Wxmpay.Rexception');
        $this->wechatApi = D('WechatApi');
    }

    public function index()
    {
        echo 111;
    }

    public function ajaxSendCaptcha()
    {
        // if (isset($_SESSION['ts']) && ($_SESSION['ts'] - time() <= 120)) {
        // 	$response['msg'] = '调用太过频繁!';
        // 	$response['rsc'] = C('RESPONSE.FAILED');
        // 	$this->ajaxReturn($response);
        // }
        $_SESSION['ts'] = time();
        $mobile = I('mobile', null);
        Vendor('Msg.SmsMsgService');
        $smsMsgSrv = new \SmsMsgService();
        $smsMsgSrv->sendCaptcha($mobile);
        $response['msg'] = '调用成功';
        $response['rsc'] = C('RESPONSE.SUCCESS');
        $this->ajaxReturn($response);
    }

    public function ajaxHrefCheck()
    {
        if (I('session.is_login', false)) { //已经登录
            $response['href'] = '';
            $response['msg'] = 'logined';
            $response['rsc'] = C('RESPONSE.SUCCESS');
            $this->ajaxReturn($response);
        }
        if (!empty($_SESSION['has_href_check']) && $_SESSION['has_href_check'] == 1) {
            $response['href'] = '';
            $response['msg'] = 'checked';
            $response['rsc'] = C('RESPONSE.SUCCESS');
            $this->ajaxReturn($response);
        }
        $_SESSION['has_href_check'] = 1;
        if (empty($_SESSION['wechat_openid']) && isWXBrowser()) {
            $url = I('url', null);
			$url = urldecode($url);
            $jsApi = new \JsApi_pub();
            $url = $jsApi->createAuthUserUrlForCode(urlencode($url));
            sys_debug_log("***********createAuthUserUrlForCode***********************");
            sys_debug_log($url);
            $response['href'] = $url;
            $response['msg'] = '调用成功';
            $response['rsc'] = C('RESPONSE.SUCCESS');
            $this->ajaxReturn($response);
        } else {
            $response['href'] = '';
            $response['msg'] = 'no openid or no wxBrowser';
            $response['rsc'] = C('RESPONSE.SUCCESS');
            $this->ajaxReturn($response);
        }
    }

    public function ajaxGetWxJSSDK()
    {
        //微信jsapi_ticket
        $url = I('url', null);
        $url = urldecode($url);
        $jsapi_ticket = "";
        $jsapi_ticket = $this->wechatApi->getJsapiTicket();
        $signatureArr = $this->wechatApi->getJsapiSignature($jsapi_ticket, $url);
        $signatureArr['appId'] = C('WXMPAY_CONFIG.APPID');
        $signatureArr['jsApi1'] = "onMenuShareTimeline";
        $signatureArr['jsApi2'] = "onMenuShareAppMessage";
        $response['data'] = $signatureArr;
        $response['msg'] = '获取成功';
        $response['rsc'] = C('RESPONSE.SUCCESS');
        $this->ajaxReturn($response);
    }
}

;